using System;
using System.Reflection;
using NodeCanvas.Variables;
using UnityEngine;

namespace NodeCanvas.Conditions
{
	[Category("✫ Script Control")]
	[Description("Check a field on a script and return if it's equal or not to a value")]
	[AgentType(typeof(Transform))]
	public class CheckField : ConditionTask
	{
		public BBVariableSet checkSet = new BBVariableSet();

		[SerializeField]
		private string fieldName;

		[SerializeField]
		private string scriptName;

		[SerializeField]
		private CompareMethod comparison;

		private Component script;

		private FieldInfo field;

		protected override string info
		{
			get
			{
				if (string.IsNullOrEmpty(fieldName))
				{
					return "No Field Selected";
				}
				return string.Format("{0}.{1}{2}", base.agentInfo, fieldName, (checkSet.selectedType != typeof(bool)) ? (TaskTools.GetCompareString(comparison) + checkSet.ToString()) : string.Empty);
			}
		}

		protected override string OnInit()
		{
			script = base.agent.GetComponent(scriptName);
			if (script == null)
			{
				return "Missing Component '" + scriptName + "' on Agent '" + base.agent.gameObject.name + "'";
			}
			field = script.GetType().NCGetField(fieldName);
			if (field == null)
			{
				return "Missing Field Info";
			}
			return null;
		}

		protected override bool OnCheck()
		{
			if (checkSet.selectedType == typeof(float) || checkSet.selectedType == typeof(int))
			{
				return TaskTools.Compare((IComparable)field.GetValue(script), (IComparable)checkSet.objectValue, comparison);
			}
			return object.Equals(field.GetValue(script), checkSet.objectValue);
		}
	}
}
